package com.community.manage.controller;

import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSONObject;
import com.community.common.entity.GroupGood;
import com.community.common.entity.GroupGoodSku;
import com.community.common.entity.vo.SendSmsParamVo;
import com.community.common.entity.vo.UserLoginVo;
import com.community.common.result.Result;
import com.community.manage.entity.vo.GroupGoodVo;
import com.community.manage.service.ManageService;
import com.community.manage.service.impl.ImformtionImpl;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.rabbit.core.RabbitMessagingTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RequestMapping("/manage")
@RestController
@Log4j2
public class ManageController {

    @Autowired
    ManageService manageService;

    @Autowired
    HttpServletRequest request;

    @Autowired
    RedisTemplate<String,Object> redisTemplate;

    @Autowired
    RabbitMessagingTemplate messagingTemplate;


    /**
     * 添加拼团商品
     * @return
     */
    @PostMapping("/addGroupGood")
    @Transactional
    public Result<String> addGroupGood(@RequestBody GroupGoodVo groupGood){
        log.info("请求路径：【{}】，请求方式：【{}】，请求参数：【{}】",
                request.getRequestURI(),request.getMethod(), JSONObject.toJSONString(groupGood));

        // 添加拼团商品
        Integer groupId = manageService.addGroupGood(groupGood);
        // 延迟队列
        GroupGoodVo groupGoodVo = new GroupGoodVo();
        groupGoodVo.setMsgId(RandomUtil.randomNumbers(10));
        groupGoodVo.setId(groupGood.getId());
        messagingTemplate.convertAndSend("group-direct","group.dead.queue",JSONObject.toJSONString(groupGoodVo));

        // 添加拼团sku详情
        manageService.addGroupSku(groupGood.getGoodSkuVoList(),groupGood.getId());

        Result<String> result = Result.success("添加成功");
        log.info("请求路径：【{}】，请求方式：【{}】，响应结果：【{}】",
                request.getRequestURI(),request.getMethod(), result);
        return result;
    }

    /**
     * 删除拼团商品
     * @param groupId
     * @return
     */
    @PostMapping("/delGroupGood/{groupId}")
    @Transactional
    public Result<String> delGroupGood(@PathVariable String groupId){
        log.info("请求路径：【{}】，请求方式：【{}】，请求参数：【{}】",
                request.getRequestURI(),request.getMethod(), groupId);

        //删除拼团商品详情
        manageService.deleteGroupGoodSku(groupId);

        // 删除拼团商品
        manageService.deleteGroupGood(groupId);

        Result<String> result = Result.success("删除成功");
        log.info("请求路径：【{}】，请求方式：【{}】，响应结果：【{}】",
                request.getRequestURI(),request.getMethod(), result);
        return result;
    }

    /**
     * 修改拼团商品信息
     * @param groupGoodVo
     * @return
     */
    @PostMapping("/updateGroupGood")
    @Transactional
    public Result<String> updateGroupGood(@RequestBody GroupGoodVo groupGoodVo){
        log.info("请求路径：【{}】，请求方式：【{}】，请求参数：【{}】",
                request.getRequestURI(),request.getMethod(), JSONObject.toJSONString(groupGoodVo));

        // 修改拼团商品
        manageService.updateGroupGood(groupGoodVo);

        // 修改拼团商品sku详情
        manageService.updateGroupGoodSku(groupGoodVo.getGoodSkuVoList());

        Result<String> result = Result.success("修改成功");
        log.info("请求路径：【{}】，请求方式：【{}】，响应结果：【{}】",
                request.getRequestURI(),request.getMethod(), result);
        return result;
    }

}
